package com.stu.student_back_end.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.stu.student_back_end.common.Result;
import com.stu.student_back_end.controller.dto.CountDTO;
import com.stu.student_back_end.controller.dto.PageDTO;
import com.stu.student_back_end.entity.TblAccount;
import com.stu.student_back_end.entity.TblBuilding;
import com.stu.student_back_end.mapper.TblBuildingMapper;
import com.stu.student_back_end.service.ITblAccountService;
import com.stu.student_back_end.service.ITblBuildingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
public class TblBuildingServiceImpl extends ServiceImpl<TblBuildingMapper, TblBuilding> implements ITblBuildingService {

    @Autowired
    private ITblAccountService accountService;

    @Override
    public Page<TblBuilding> queryBuildingPage(PageDTO pageDTO) {
        Page<TblBuilding> page =new Page<>(pageDTO.getCurrentPage(),pageDTO.getSizePage());
        Page<TblBuilding> buildPage = new Page<>();
        //判断是否带关键字
        if(!(pageDTO.getKeyWord() == "")) {
            //不为空
            buildPage = page(page, new LambdaQueryWrapper<TblBuilding>().like(TblBuilding::getBuildName, pageDTO.getKeyWord()));
        }else {
            buildPage = page(page);
        }
        return buildPage;
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public Result saveBuildingAndAccount(TblBuilding building) {

        //保存楼栋信息
        boolean save = save(building);

        //设置account表的build字段
        boolean update = accountService.update(new LambdaUpdateWrapper<TblAccount>()
                .eq(TblAccount::getName, building.getManager())
                .set(TblAccount::getBuild, building.getBuildName()));

        //查询为空
        if(!save && !update) new RuntimeException();

        //查询不为空
        return Result.success("保存成功！");
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public Result updateBuildingAndAccount(TblBuilding tblBuilding) {

        //修改楼栋信息
        boolean updateById = updateById(tblBuilding);

        //删除account中其他字段中的该楼栋
        boolean update1 = accountService.update(new LambdaUpdateWrapper<TblAccount>()
                .eq(TblAccount::getBuild, tblBuilding.getBuildName())
                .set(TblAccount::getBuild, "无"));

        //修改account表的字段
        boolean update = accountService.update(new LambdaUpdateWrapper<TblAccount>()
                .eq(TblAccount::getName, tblBuilding.getManager())
                .set(TblAccount::getBuild, tblBuilding.getBuildName()));

        //判断是否修改成功
        if(!updateById && !update  && !update1) throw new RuntimeException();

        //修改成功
        return Result.success("修改成功！");
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public Result delByIdBuildingAndAccount(TblBuilding tblBuilding) {

        //删除楼栋记录
        boolean removeById = removeById(tblBuilding.getId());

        //删除account表中build字段的值
        boolean update = accountService.update(new LambdaUpdateWrapper<TblAccount>()
                .eq(TblAccount::getBuild, tblBuilding.getBuildName())
                .set(TblAccount::getBuild, "无"));

        //判断是否删除成功
        if(!removeById && !update) throw new RuntimeException();

        //删除成功
        return Result.success("删除成功！");
    }

    @Override
    public CountDTO queryBuildingCount() {
        CountDTO countVo = new CountDTO();
        countVo.setCount(baseMapper.queryBuildingCount());
        countVo.setName("楼栋");
        return countVo;
    }
}
